home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 151_01 / ed3.c < prev    next >
Text File  |  1985-03-09  |  18KB  |  949 lines

  1. /* ED3.C */
  2.  
  3. #include "ed0.c"
  4. #include "ed1.ccc" 
  5. int readfile = -1;    /* separate read and write files */
  6. int writefile = -1;    /* -1 for inactive, file channel no. for active */
  7. char rfilename[SYSFNMAX];    /* file names */
  8. char wfilename[SYSFNMAX];
  9. char databuf[MAXLEN1];        /* single buffer replaces redundant buffers */
  10. fileclear()        /* initializes filenames to zero length */
  11. {
  12.     rfilename[0] = EOS;
  13.     wfilename[0] = EOS;
  14. }
  15. append(args) char *args;
  16. {
  17. int file;
  18. int n;
  19. int topline;
  20. char locfn[SYSFNMAX];
  21.     if (name1(args,locfn)==ERR) {
  22.         return;
  23.     }
  24.     if (locfn[0]==EOS) {
  25.         message("no file argument");
  26.         return;
  27.     }
  28.     if ((file=sysopen(locfn,"r"))==ERR) {
  29.         message("file not found");
  30.         return;
  31.     }
  32.     while ((n=readline(file,databuf,MAXLEN))>=0) {
  33.         if (n>MAXLEN) {
  34.             message("line truncated");
  35.             n=MAXLEN;
  36.         }
  37.         if (bufins(databuf,n)==ERR) {
  38.             break;
  39.         }
  40.         if (bufdn()==ERR) {
  41.             break;
  42.         }
  43.     }
  44.     sysclose(file);
  45.     topline=max(1,bufln()-SCRNL2);
  46.     bufout(topline,2,SCRNL2);
  47.     bufgo(topline);
  48. }
  49. change(args,begin,end) char *args; int begin,end;
  50. {
  51. char oldline[MAXLEN1];
  52. char newline[MAXLEN1];
  53. char oldpat[MAXLEN1];
  54. char newpat[MAXLEN1];
  55. int from, to, col, n, k;
  56.     if (get2args(args,&from,&to)==ERR) {
  57.         if (check2mark(begin,end,&from,&to)==ERR){
  58.             sysabort();
  59.             return;
  60.         }
  61.     }
  62.     scr_chr_attr(SETINTENSE);
  63.     fmtsout("search mask ?    ",0);
  64.     getcmnd(oldpat,15);
  65.     fmtcrlf();
  66.     if(oldpat[0]==EOS) {
  67.         return;
  68.     }
  69.     pmtline();
  70.     scr_chr_attr(SETINTENSE);
  71.     fmtsout("change mask ?    ",0);
  72.     getcmnd(newpat,15);
  73.     fmtcrlf();
  74.     while (from<=to) {
  75.         if (chkkey()==YES) {
  76.             break;
  77.         }
  78.         if (bufgo(from++)==ERR) {
  79.             sysabort();
  80.             break;
  81.         }
  82.         if (bufatbot()==YES) {
  83.             sysabort();
  84.             break;
  85.         }
  86.         n=bufgetln(oldline,MAXLEN);
  87.         n=min(n,MAXLEN);
  88.         oldline[n]=EOS;
  89.         if (oldpat[0]=='^') {
  90.             if (amatch(oldline,oldpat+1,NO)==YES) {
  91.                 k=replace(oldline,newline,
  92.                     oldpat+1,newpat,0);
  93.                 if (k==ERR) {
  94.                     sysabort();
  95.                     return;
  96.                 }
  97.                 fmtcrlf();
  98.                 putdec(bufln(),5);
  99.                 fmtsout(newline,5);
  100.                 outdeol();
  101.                 bufrepl(newline,k);
  102.             }
  103.             continue;
  104.         }
  105.         if ((col=amatch(oldline,oldpat,YES))>=0) {
  106.             k=replace(oldline,newline,
  107.                 oldpat,newpat,col);
  108.             if (k==ERR) {
  109.                 sysabort();
  110.                 return;
  111.             }
  112.             fmtcrlf();
  113.             putdec(bufln(),5);
  114.             fmtsout(newline,5);
  115.             outdeol();
  116.             bufrepl(newline,k);
  117.         }
  118.     }
  119.     fmtcrlf();
  120. }
  121. clear()
  122. {
  123.     if (chkbuf()==YES) {
  124.         outclr();
  125.         outxy(0,SCRNL1);
  126.         bufnew();
  127.         message("buffer cleared");
  128.         return(YES);
  129.     }
  130.     else{
  131.         return(NO);
  132.     }
  133. }
  134. delete(args,begin,end) char *args; int begin,end;
  135. {
  136. int from, to;
  137.     if(get2args(args,&from,&to)==ERR) {
  138.         if (check2mark(begin,end,&from,&to)==ERR){
  139.             sysabort();
  140.             return;
  141.         }
  142.     }
  143.     if (from>to) {
  144.         return;
  145.     }
  146.     if (edpick(from,to)==OK){
  147.         message ("deleted lines saved in pick buffer");
  148.     }
  149.     if (bufgo(from)==ERR) {
  150.         return;
  151.     }
  152.     if (bufndel(to-from+1)==ERR) {
  153.         sysabort();
  154.         return;
  155.     }
  156.     bufout(bufln(),1,SCRNL1);
  157. }
  158. find()
  159. {
  160.     return(suurch(bufln()+1,HUGE,YES));
  161. }
  162. list(args,begin,end) char *args; int begin,end;
  163. {
  164. int n;
  165. int from, to, line, oldline;
  166.     oldline=bufln();
  167.     if (get2args(args,&from,&to)==ERR) {
  168.         if (check2mark(begin,end,&from,&to)==ERR) {
  169.             sysabort();
  170.             return;
  171.         }
  172.     }
  173.     line=from;
  174.     while (line<=to) {
  175.         fmtassn(NO);
  176.         if (chkkey()==YES) {
  177.             break;
  178.         }
  179.         fmtassn(YES);
  180.         if (bufgo(line++)!=OK) {
  181.             break;
  182.         }
  183.         if (bufatbot()) {
  184.             sysabort();
  185.             break;
  186.         }
  187.         n=bufgetln(databuf,MAXLEN1);
  188.         n=min(n,MAXLEN);
  189.         databuf[n]=CR;
  190.         fmtsout(databuf,0);
  191.         fmtcrlf();
  192.     }
  193.     fmtassn(NO);
  194.     bufgo(oldline);
  195. }
  196. extract(args,begin,end) char *args; int begin,end;
  197.     /* write indicated line range to writefile */
  198. {
  199. /*char args1[SCRNW1];
  200. char *argp;
  201. int file;
  202. char locfn[SYSFNMAX];    */
  203. int oldline, from, to, n;
  204.     if (writefile == -1) {
  205.         message("file not opened");
  206.         return;
  207.     }
  208.     oldline = bufln();
  209.     if (get2args(args,&from,&to)==ERR) {/* from and to are line range */
  210.         if (check2mark(begin,end,&from,&to)==ERR){
  211.             return;
  212.         }
  213.     }
  214. /*    message("enter file name");  */ /* request the file name */
  215. /*    getcmnd(args1,0);
  216.     argp=skipbl(args1);
  217.     if (syschkfn(argp)==ERR) {
  218.         return;
  219.     }
  220.     syscopfn(argp,locfn);
  221.     if (locfn[0]==EOS) {
  222.         message("no file argument");
  223.         return;
  224.     }
  225.     if ((file=sysopen(locfn,"w"))==ERR){
  226.         return;
  227.     }    */
  228.     if (bufgo(from)==ERR){
  229.        /*      sysclose(file);    */
  230.         return;
  231.     }
  232.     while ((bufatbot()==NO)&(bufln()<=to)) {    /* do the writing */
  233.         n=bufgetln(databuf,MAXLEN);
  234.         n=min(n,MAXLEN);
  235.         if (pushline(writefile,databuf,n)==ERR){
  236.             break;
  237.         }
  238.         if (bufdn()==ERR) {
  239.             break;
  240.         }
  241.     }
  242.     bufgo(oldline);
  243. /*      sysclose(file);    */  /* close the file */
  244. }
  245. openf (args,flag) char *args; int flag;
  246. {
  247. /* open a file for reading */
  248. char locfn [SYSFNMAX];
  249. int n;
  250. int file;
  251. int topline;
  252.     if (readfile > 0) {        /* check for open readfile */
  253.         message("read file still open");
  254.         return;
  255.     }
  256.     if (flag==YES) {
  257.         if (name1(args,locfn)==ERR) {
  258.             return;
  259.         }
  260.     }
  261.     else  {
  262.         if (name3(args,locfn)==ERR) {
  263.             return;
  264.         }
  265.     }
  266.     if (locfn[0]==EOS) {
  267.         message("no file argument");
  268.         return;
  269.     }
  270.     if (chkbuf()==NO) {
  271.         return;
  272.     }
  273.     if ((file=sysopen(locfn,"r"))==ERR) {
  274.         message("file not found");
  275.         return;
  276.     }
  277.     syscopfn(locfn, rfilename);    /* make the file name the readfile */
  278.     readfile = file;
  279.     pmtrfile(rfilename);
  280.     bufnew();
  281. }
  282. getit (args) char *args;    /* new: add n lines to buffer if room */
  283. {
  284. int n;
  285. int topline;
  286. int nlines,npoint;
  287.     if (readfile == -1) {
  288.         message ("no read file");
  289.         return;
  290.     }
  291.     pmtrfile(rfilename);
  292.     if (getarg(args,&nlines)==ERR) {
  293.         return;
  294.     }
  295.     if (nlines<1) {
  296.         return;
  297.     }
  298.     if (bufgo(HUGE)==ERR) {
  299.         return;
  300.     }
  301.     npoint=1;
  302.     while (npoint<=nlines) {    /* add the lines */
  303.         npoint++;
  304.         if ((n=readline(readfile,databuf,MAXLEN))>=0) {
  305.             if (n>MAXLEN) {
  306.                 message("line truncated");
  307.                 n=MAXLEN;
  308.             }
  309.             if (buf1ins(databuf,n)==ERR) {
  310.                 bufgo(1);
  311.                 topline=max(1,bufln()-SCRNL2);
  312.                 bufout(topline,2,SCRNL2);
  313.                 bufgo(topline);
  314.                 return;
  315.             }
  316.             if (bufdn()==ERR) {
  317.                 break;
  318.             }
  319.         }
  320.         else {
  321.             npoint = HUGE;       /* reached end of file - close it */
  322.             sysclose(readfile);
  323.             readfile = -1;
  324.             rfilename[0] = EOS;
  325.             pmtrfile(rfilename);
  326.         }
  327.     }
  328.     bufgo(1);
  329.     topline=max(1,bufln()-SCRNL2);
  330.     bufout(topline,2,SCRNL2);
  331.     bufgo(topline);
  332. }
  333. rest (args) char *args; /* new: try to read rest of readfile into buffer */
  334. {
  335. int n;
  336. int topline;
  337.     if (readfile == -1) {
  338.         message ("no read file");
  339.         return;
  340.     }
  341.     pmtrfile(rfilename);
  342. /* if buffer has been changed and not saved, give option of clearing, */
  343. /* otherwise add to end of buffer */
  344.     if(bufchng()==YES) {
  345.         scr_chr_attr(SETINTENSE);
  346.  
  347.         fmtsout("buffer not saved. clear? ",0);
  348.         scr_chr_attr(SETFAINT);
  349.         pmtline();
  350.         if (tolower(syscout(syscin()))=='y'){
  351.             outclr();
  352.             outxy(0,SCRNL1);
  353.             bufnew();
  354.             message("buffer cleared");
  355.         }
  356.         else {
  357.             bufgo(HUGE);
  358.         }
  359.     }
  360.     else{
  361.         outclr();
  362.         outxy(0,SCRNL1);
  363.         bufnew();
  364.         message("buffer cleared");
  365.  
  366.     }
  367.     while ((n=readline(readfile,databuf,MAXLEN))>=0) {
  368.         if (n>MAXLEN) {
  369.             message("line truncated");
  370.             n=MAXLEN;
  371.         }
  372.         if (buf1ins(databuf,n)==ERR) {
  373.             bufgo(1);
  374.             topline=max(1,bufln()-SCRNL2);
  375.             bufout(topline,2,SCRNL2);
  376.             bufgo(topline);
  377.             return;
  378.         }
  379.         if (bufdn()==ERR) {
  380.             break;
  381.         }
  382.     }
  383.     sysclose(readfile);    /* close file if all read */
  384.     readfile = -1;
  385.     rfilename[0] = EOS;
  386.     pmtrfile(rfilename);
  387.     bufgo(1);
  388.     topline=max(1,bufln()-SCRNL2);
  389.     bufout(topline,2,SCRNL2);
  390.     bufgo(topline);
  391. }
  392. rename(args) char *args;    /* new: change name of writefile */
  393. {
  394.     if (writefile == -1) {
  395.         message("no write file - call name");
  396.         return;
  397.     }
  398.     sysclose(writefile);
  399.     writefile = -1;
  400.     wfilename[0]=EOS;
  401.     pmtwfile(wfilename);
  402.     if (name0(args,wfilename)==ERR) {
  403.         return;
  404.     }
  405.     pmtwfile(wfilename);
  406. }
  407. name(args) char *args;        /* altered definition: name the writefile */
  408. {
  409.     if (writefile > 0) {
  410.         message("write file open");
  411.         return;
  412.     }
  413.     if (name0(args,wfilename)==ERR){
  414.         return;
  415.     }
  416.     pmtwfile(wfilename);
  417. }
  418. name0(args,wfilename) char *ar